<?
require_once('functions.php');

	function getStatus()
	{
		$conn = getConnection();
		return $conn->query('SELECT * FROM Status');
	}
	
	function getAStatus($id)
{
        $conn = getConnection();
        $results = $conn->query("SELECT * FROM Status WHERE status_ID=$id");
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function blankStatus()
{
        return array('status_ID'=>null,'statusName'=>null);
}

	function saveStatus(&$row)

{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['status_ID']))
        {
                $sql =  "Insert Status (statusName) "
                        .       " Values ('$row2[statusName]') ";
        }else{
                $sql =  "UPDATE Status "
                        .       "Set statusName='$row2[statusName]'WHERE status_ID=$row2[status_ID] ";
        }
        $results = $conn->query($sql);
        $error = $conn->error;
        if(empty($row['status_ID']) && empty($error))
        {
                $row['status_ID'] = $conn->insert_id;
        }
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}

function getInsertedStatus()
{
	$conn = getConnection();
	$query = "SHOW TABLE STATUS LIKE 'Status'";
	$res = $conn->query($query);
	$row = $res->fetch_assoc();
	
	return $row['Auto_increment'] - 1;
}

function addStatus($statusName)
{
		$conn = getConnection();
		return $conn->query("INSERT INTO Status VALUES (NULL,'$statusName')");			
}


	function deleteStatus($id)
{
		$conn = getConnection();
		$conn->query("DELETE FROM Status WHERE status_ID = $id");
		$query = "SHOW TABLE STATUS LIKE 'Status'";
		$res = $conn->query($query);
		$row = $res->fetch_assoc();
		$autoInc = $row['Auto_increment'] - 1;
		$conn->query("ALTER TABLE Status AUTO_INCREMENT = $autoInc");
		$conn->close();
		header('Location: index.php');
}	

